/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pt.regcolm.servico;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import pt.regcolm.entities.Medicao;
import pt.regcolm.sqlcon.BDPathCon;

/**
 * REST Web Service
 *
 * @author MoLt1eS
 */
@Path("registoDados")
public class RegistoDadosResource {

    private final static BDPathCon myCon = BDPathCon.getBDPathCon();
    
    @Context
    private UriInfo context;

    /**
     * Creates a new instance of RegistoDadosResource
     */
    public RegistoDadosResource() {
    }

    /**
     * Retrieves representation of an instance of pt.regcolm.servico.RegistoDadosResource
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("text/plain")
    public String getText(
            @DefaultValue("0") @QueryParam("lat") float latitude,
            @DefaultValue("0") @QueryParam("lon") float longitude,
            @DefaultValue("0") @QueryParam("nAb") int nAbelhas,
            @DefaultValue("0") @QueryParam("peso") double peso,
            @DefaultValue("0") @QueryParam("tInt") double tInt,
            @DefaultValue("0") @QueryParam("tExt") double tExt,
            @DefaultValue("0") @QueryParam("hInt") double hInt) {
        
        return  "Latitude(lat): "+latitude+
                "\nLongitude(lon): "+longitude+
                "\nNumero Abelhas(nAb): "+nAbelhas+
                "\nPeso(peso): "+peso+
                "\nTemperatura Interior(tInt): "+tInt+
                "\nTemperatura Exterior(tExt): "+tExt+
                "\nHumidade Interna(hInt): "+hInt;
    }

    /**
     * PUT method for updating or creating an instance of RegistoDadosResource
     * @param content representation for the resource
     * @param valor
     * @return an HTTP response with content of the updated or created resource.
     */
    @POST
    @Produces("text/plain")
    @Consumes("application/json")
    public String putText(String content,
                        @DefaultValue("0") @QueryParam("lat") float latitude,
                        @DefaultValue("0") @QueryParam("lon") float longitude,
                        @DefaultValue("0") @QueryParam("nAb") int nAbelhas,
                        @DefaultValue("0") @QueryParam("peso") double peso,
                        @DefaultValue("0") @QueryParam("tInt") double tInt,
                        @DefaultValue("0") @QueryParam("tExt") double tExt,
                        @DefaultValue("0") @QueryParam("hInt") double hInt) {
        System.out.println(content);
        
        try {
            
            
            Statement st = (Statement) myCon.getCon().createStatement();
            //PreparedStatement sStmt = myCon.getCon().prepareStatement("INSERT INTO medidas(medida) VALUES('"+value+"')");
            st.executeUpdate("INSERT INTO `medidas`.`medicao`\n" +
                            "(`latitude`,\n" +
                            "`longitude`,\n" +
                            "`nAbelhas`,\n" +
                            "`peso`,\n" +
                            "`tInterior`,\n" +
                            "`tExterior`,\n" +
                            "`hInterior`)\n" +
                            "VALUES\n" +
                            "('"+latitude+"',\n" +
                            "'"+longitude+"',\n" +
                            "'"+nAbelhas+"',\n" +
                            "'"+peso+"',\n" +
                            "'"+tInt+"',\n" +
                            "'"+tExt+"',\n" +
                            "'"+hInt+"');");
            
            return "OK";
        } catch (SQLException ex) {
            Logger.getLogger(RegistoResource.class.getName()).log(Level.SEVERE, null, ex);
            return "FAIL";
        }
    }
}
